import React  from 'react';
import { Form, Button, message } from 'antd';
import { ModalForm, ProFormTextArea } from '@ant-design/pro-components';
import { useRequest } from 'ahooks';
import { ArticleStatus } from "common/utils/data";
import { articleV1AdminControllerUpdate } from "@/services/auto/articleAdmin";

interface Props {
  uuid: string;
  reload?: () => void;
}

export default function Reject(props: Props){
  const { uuid, reload } = props;

  const [form] = Form.useForm<FormValues>();

  const { runAsync: patchRun } = useRequest(articleV1AdminControllerUpdate, {
    manual: true,
    onSuccess: () => {
      message.success('操作成功');
      reload?.();
    },
  });

  return (
    <ModalForm
      form={form}
      onOpenChange={(val) => {
        if (val) {
        } else {
          form.resetFields();
        }
      }}
      title="驳回"
      modalProps={{
        maskClosable: false,
        width: 500,
      }}
      trigger={<Button danger>驳回</Button>}
      onFinish={(values: FormValues) => {
        return patchRun({uuid},{
          status: ArticleStatus.REJECT,
          rejectReason: values.rejectReason,
        })
          .then(() => {
            return true;
          })
          .catch(() => {
            return false;
          });
      }}
    >
      <ProFormTextArea
        name="rejectReason"
        label="拒绝原因"
        rules={[{ required: true, message: '请输入' }]}
      />
    </ModalForm>
  );
};

interface FormValues{
  rejectReason: string
}
